package com.task.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.task.common.BaseHibernateDao;
import com.task.dao.TaskDao;
import com.task.entity.Task;

public class TaskDaoImpl extends BaseHibernateDao<Task> implements TaskDao {

	public List<Task> getTaskOfNoType() {
		 Session session=super.getSession();
		 String hql="from Task task where task.taskType=0 and task.task is null";
		 Query query=session.createQuery(hql);
		 return query.list();
	}

	public List<Task> getTaskByDateAndType(Integer userId, String date,
			int taskType,int taskState) {
	   	 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	   	 Date start=null;
		try {
			start = sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   	 Date end=new Date(start.getTime()+24*60*60*1000);
	   	 SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		 Session session=super.getSession();
		 String hql="from Task task where task.taskUser.userId=:id and task.taskType=:type and task.taskState=:state and task.taskDate >= '"+sim.format(start)+"' and task.taskDate < '"+sim.format(end)+"'";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", userId);
		 query.setInteger("type", taskType);
		 query.setInteger("state", taskState);
		 return query.list();
	}

	public void updateTaskStateByTaskId(Integer taskId, int taskState) {
		 Session session=super.getSession();
		 String hql="update Task task set task.taskState=:state where  task.taskId=:id";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", taskId);
		 query.setInteger("state", taskState);
		 query.executeUpdate();
		
	}

	public List<Task> getFuture5Tasks(Integer userId) {
		 Session session=super.getSession();
		 Date   today   =   new   Date(); 
		 Date   tommorow   =   new   Date(today.getTime()+24*60*60*1000);
		 Date end=new Date(today.getTime()+24*60*60*1000*6);
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		 String hql="from Task task where task.taskUser.userId=:id and task.taskDate>= '"+sdf.format(tommorow)+"' and task.taskDate<'"+sdf.format(end)+"' order by task.taskDate";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", userId);
		 return query.list();
	}

	public void updateNavToMenuNav(Integer taskId, int parentId) {
		 Session session=super.getSession();
		 String hql="update Task task set task.taskType=0,task.task.taskId=:parentId,task.taskDate='' where task.taskId=:id";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", taskId);
		 query.setInteger("parentId", parentId);
		 query.executeUpdate();
		
	}

	public void updateNavToOnelistNav(Integer taskId, String date, int taskType) {
		 Session session=super.getSession();
		 String hql="update Task task set task.taskType=:type,task.taskDate=:date,task.task=null where task.taskId=:id";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", taskId);
		 query.setInteger("type", taskType);
		 query.setString("date", date);
		 query.executeUpdate();
		
	}

	public void updateNavToMenuTitle(Integer taskId) {
		 Session session=super.getSession();
		 String hql="update Task task set task.taskType=0,task.taskDate='',task.task=null where task.taskId=:id";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", taskId);
		 query.executeUpdate();
		
	}

	public void updateMenuTitleToOnelistNav(Integer taskId, String date,
			int taskType) {
		 Session session=super.getSession();
		 String hql="update Task task set task.taskType=:type,task.taskDate=:date,task.task=null where task.taskId=:id";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", taskId);
		 query.setInteger("type", taskType);
		 query.setString("date", date);
		 query.executeUpdate();
		
	}

	public void updateMenuTitleToNav(Integer taskId, int parentId) {
		Session session=super.getSession();
		 String hql="update Task task set task.taskType=0,task.task.taskId=:parentId where task.taskId=:id";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", taskId);
		 query.setInteger("parentId", parentId);
		 query.executeUpdate();
		
	}

	public Integer addTask(Task task) {
		return (Integer) super.add(task);
		
	}

	public void delTaskById(Integer taskId) {
		 super.deleteById(taskId);
		
	}

	public Task getTaskById(Integer taskId) {
		return super.getById(taskId);
	}

	public void updateTaskName(Integer taskId, String taskName) {
		 Session session=super.getSession();
		 String hql="update Task task set task.taskName=:name where task.taskId =:id";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", taskId);
		 query.setString("name", taskName);
		 query.executeUpdate();
	}

	public void delTaskByParentId(Integer parId) {
		 Session session=super.getSession();
		 String hql="delete from  Task task where task.task.taskId =:id";
		 Query query=session.createQuery(hql);
		 query.setInteger("id", parId);
		 query.executeUpdate();
	}

	public List<Task> getPreTask() {
		 Date   today   =   new   Date(); 
		 Date   tommorow   =   new   Date(today.getTime()+24*60*60*1000);
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		 Session session=super.getSession();
		 String hql="from Task task where task.taskDate >= '"+sdf.format(tommorow)+"' order by task.taskDate asc";
		 Query query=session.createQuery(hql);
		 return query.list();
	}

	public void updateTaskToTomorrow() {
		 Date   today   =   new   Date(); 
		 Date   tommorow   =   new   Date(today.getTime()+24*60*60*1000);
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		Session session=super.getSession();
		String hql="update Task task set task.delay=1,task.taskDate='"+sdf.format(tommorow)+"' where task.taskDate >='"+sdf.format(today)+"' and task.taskDate<'"+sdf.format(tommorow)+"'";
		Query query=session.createQuery(hql);
		query.executeUpdate();
	}

}
